
# -*- coding: utf-8 -*-
from django.contrib.auth.decorators import login_required
from django.http import HttpResponse
from django.http import HttpResponseRedirect
from django.utils.translation import ugettext as _
from ragendja.template import render_to_response
from django.template import Context, RequestContext, loader
from django.views.generic.list_detail import object_list
from django.views.generic.simple import direct_to_template
from django.forms.formsets import formset_factory
from haco.models import *
from httpauth import *
from haco.jsTime import *
from haco.convert import *
import logging
import time

@login_required
def repo_all( request):

    u = User.all()
    u = u.filter( 'username', request.user.username)
    u = u.filter( 'is_superuser', True)

    if u.count() == 0:
        return HttpResponse("you don't have the right to access this page")
    else:
        logging.info("repo_all")
        if request.GET.has_key( 'delta'):
            day = someday(int(request.GET['delta']))
        else:
            day = today()

        q = HacoUser.all()

        mes = "<html><head></head><body>"
        for hacoUser in q:
            user = hacoUser.user
        
            mes += str(hacoUser.user.username)
            mes +="<div>"
            t_list = [-1.0] * 144
            l_list = [-1.0] * 144
            w_list = [-1.0] * 144
             
            q =Haco.all()
            q =q.order( 'pnum')
            q =q.filter( 'user', user)
            q =q.filter( 'date', day)
            q =q.order( 'pnum')
            
            for h in q:
                t_list[h.pnum] = h.temp
                l_list[h.pnum] = h.light
                w_list[h.pnum] = h.watt

            mes += t_chart(t_list)
            mes += l_chart(l_list)
            mes += w_chart(w_list)

            mes +="</div>"
        mes +="</body></html>"
        return HttpResponse( mes)

def t_chart( a_list):
    chart = "<img src='http://chart.apis.google.com/chart?chs=750x185&cht=lc&chco=00b379&chxt=y&chxr=0,0,40&chds=0,40&chd=t:"
    for data in a_list:
        chart += str(data) +","
    chart = chart[0:len(chart)-1]
    chart += "&chtt=temperature&chg=4.17,25,1,4&chl=0|||||1|||||2|||||3|||||4|||||5|||||6|||||7|||||8|||||9|||||10|||||11|||||12|||||13|||||14|||||15|||||16|||||17|||||18|||||19|||||20|||||21|||||22|||||23|||||'>"

    return chart

def l_chart( a_list):
    chart = "<img src='http://chart.apis.google.com/chart?chs=750x185&cht=lc&chxt		=y&chxr=0,0,700&chds=0,700&chd=t:"
    for data in a_list:
        chart += str(data) +","
    chart = chart[0:len(chart)-1]
    chart += "&cht=lc&chtt=light&chg=4.17,14.3,1,2&chl=0|||||1|||||2|||||3|||||4|||||5|||||6|||||7|||||8|||||9|||||10|||||11|||||12|||||13|||||14|||||15|||||16|||||17|||||18|||||19|||||20|||||21|||||22|||||23|||||'>"
    return chart

def w_chart( a_list):
    chart = "<img src='http://chart.apis.google.com/chart?chs=750x185&cht=lc&chco=009CD1&chxt=y&chxr=0,0,1000&chds=0,1000&chd=t:"
    for data in a_list:
        chart += str(data) +","
    chart = chart[0:len(chart)-1]
    chart += "&cht=lc&chtt=watt&chg=4.17,20.00,1,2&chl=0|||||1|||||2|||||3|||||4|||||5|||||6|||||7|||||8|||||9|||||10|||||11|||||12|||||13|||||14|||||15|||||16|||||17|||||18|||||19|||||20|||||21|||||22|||||23|||||'>"

    return chart



